package 算法;

public class 反转链表 {
    public static void main(String[] args) {

        ListNodes ls = new 反转链表().reverseList(new ListNodes(1,new ListNodes(2,new ListNodes(3,new ListNodes(4,new ListNodes(5))))));
        while (ls != null){
            System.out.println(ls.val);
            ls = ls.next;
        }
    }
    public ListNodes reverseList(ListNodes head) {
        if(head == null || head.next == null) return head;

        ListNodes ln = head;
        while (ln.next.next != null){
            ln = ln.next;
        }
        //ln就是倒数第二个
        ListNodes end = ln.next;
        ln.next = end.next;
        end.next = reverseList(head);

        return  end;

    }
}
 class ListNodes {
      int val;
      ListNodes next;
      ListNodes(int x) { val = x; }
     ListNodes(int x,ListNodes next ) { val = x;this.next = next; }
}
